package com.kangye.exam.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Itembank;
import com.kangye.exam.bean.Paper;
import com.kangye.exam.service.PaperService;
import com.kangye.exam.utils.JsonFileUtil;
import com.kangye.exam.utils.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.jfunc.json.Json;
import top.jfunc.json.impl.JSONObject;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/paper")
@Controller
public class PaperController {
    @Resource
    private PaperService paperService;
    /*
    * @Description 通过学科名称从题库中查出所有这个学科的题
    *
    * */

    @RequestMapping("/findsub")//按照学科查询该学科所有的题
    public Object selectsubject(String subjectName,Integer pageNum,Integer pageSize){
        PageInfo pageInfo =  paperService.selectBysubjectName(subjectName,pageNum,pageSize);
        Map<String,Object>map = new HashMap<>();
        map.put("code",0);
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("/findid")//按照题的id和类型名称进行选题
    public Object selectbyId(Integer itemId, String typeName,Integer pageNum,Integer pageSize){
        PageInfo pageInfo =  paperService.selectByid(itemId, typeName,pageNum,pageSize);
        Map<String,Object>map = new HashMap<>();
        map.put("code",0);
        map.put("data",pageInfo.getList());
        System.out.println(pageInfo.getList());
        return map;
    }
    @RequestMapping("/paper")//试题成卷
    public Object paper(String items, String paperName, Integer teacherId,Integer paperTime,String subjectName)throws IOException {
        JsonFileUtil jsonFileUtil = new JsonFileUtil();
        String paperPath=jsonFileUtil.writeLocalJson(paperName, items);
        paperService.createpaper(paperName,paperPath,paperTime,teacherId,subjectName);
        return null;
    }
    @RequestMapping("/paperall")//查看所有试卷
    public Object paperall(Integer teacherId,Integer paperId,Integer subjectId,Integer pageNum,Integer pageSize){
        PageInfo pageInfo= paperService.paperall(teacherId,paperId,subjectId,pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("/delpaper")//删除试卷
    public Object delpaper(Integer paperId){
        Integer code = paperService.delpaper(paperId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        return code;
    }

}
